package com.wms.business;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;

import org.apache.commons.lang3.time.DateUtils;
import org.apache.http.client.ClientProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.wms.client.BuildHttpClient;
import com.wms.domain.PDParam;
import com.wms.domain.PDParamPath;

public class PDBusiness2false  {
	private Logger logger = LoggerFactory.getLogger(PDBusiness2false.class);
	private final String PATTERN[] = new String[] { "yyyy-MM-dd HH:mm:ss" };
	private JSONObject result = null;

	public static String getreviewId = "";// 获取盘点单ID
	public static String getareaId = "";// 获取库区ID
	public static String getlocationId = "";// 获取货位ID
	public static String getreviewLocationId="";//获取盘点货位（任务）ID
	public static String getbarCode="";//获取商品条码
	public static String getbatchId="";//获取生产批次ID
	public static String getgoodsId="";//获取商品ID
	/**
	 * PC创建盘点单
	 */
	public void CreateMangPD() {
		try {
			logger.info("开始进行--------------PC创建盘点单-----------------");
			result = BuildHttpClient.doPost(PDParamPath.BuildPDpath, PDParamPath.locationIdList);
			logger.info("这里是\"PC盘点单管理新增或发起盘点\"响应：{}", result);
		} catch (Exception e) {

		}
	}

	/**
	 * RF 查询新增状态盘点单,获取到最新的盘点单
	 */
	public String getReviewid() throws ParseException {

		try {
			logger.info("开始进行--------------查询新增状态盘点单-----------------");
			result = BuildHttpClient.doPost(PDParamPath.Reviewidpath, PDParamPath.ReviewidbodyParams);
			logger.info("这里是\"查询新增状态盘点单\"响应：{}", result);
			// 获取result中结果
			ArrayList<PDParam> params= JSON.parseObject(result.getJSONObject("result").get("rows").toString(), new TypeReference<ArrayList<PDParam>>() {});
//			// 获取rows中结果
//			JSONArray jsonArray = result.getJSONArray("rows");
			long min = 0;
			// String getreviewId = "";
			// 当前时间
			long time1 = Calendar.getInstance().getTime().getTime();
			for (int i = 0; i < params.size(); i++) {
				// 遍历,jsonarray数组，把每一个对象转成json对象
				PDParam job = params.get(i);
				String createTime = job.getCreateTime();
				// 获取到的时间
				long time2 = DateUtils.parseDate(createTime, PATTERN).getTime();
				// System.out.println(job.get("reviewId")); // 得到 每个对象中的属性值
				String reviewId = job.getReviewId();
				long diff = time1 - time2;
				if (min == 0 || (min != 0 && diff < min)) {
					min = diff;
					getreviewId = reviewId;
				}
			}
			logger.info("这里是获取到的盘点单ID : {}", getreviewId);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return getreviewId;
	}

	/**
	 * RF查询盘点单库区信息，由上方reviewId作为参数 获取库区ID
	 * 
	 * @param reviewId
	 * 
	 */
	public String selectPDMsg(String reviewId) throws ParseException {	
		// json字符串拼接
//		StringBuilder reviewid = new StringBuilder();
//		reviewid.append("{");
//		reviewid.append("\"reviewId\":" + getreviewId);
//		reviewid.append("}");
//		String selectPDMsgbodyParams = reviewid.toString();
		PDParam param=new PDParam();
		param.setReviewId(reviewId);

		try {
			logger.info("开始进行--------------查询盘点单库区信息-----------------");
			result = BuildHttpClient.doPost(PDParamPath.selectPDMsgpath, JSON.toJSONString(param));
			logger.info("这里是\"RF查询盘点单库区信息\"响应：{}", result);		 
			JSONArray jsonArray = result.getJSONArray("result");		
			JSONObject object = (JSONObject) jsonArray.get(0);
			String areaId = object.getString("key");					
		     getareaId=areaId;
		     logger.info("提取出来的库区ID ：{}", getareaId);	
		} catch (IOException e) {
		}
		return getareaId;
	}

	/**
	 * RF领取计划盘点单
	 * 
	 */
	public String receivePD() {
		
		String receivePDPath = "/wms/review/claim";
		// json字符串拼接
		StringBuilder receiveparam = new StringBuilder();
		receiveparam.append("{");
		receiveparam.append("\"reviewId\":" + getreviewId);
		receiveparam.append(",");
		receiveparam.append("\"areaId\":" + getareaId);
		receiveparam.append("}");
		String Receiveparam = receiveparam.toString();
		try {
			logger.info("开始进行--------------RF领取计划盘点单-----------------");
			result = BuildHttpClient.doPost(receivePDPath, Receiveparam);
			logger.info("这里是\"RF领取盘点\"响应：{}", result);
			// 开始提取
			result = result.getJSONObject("result");
			getlocationId = result.getString("locationId");
			logger.info("提取出来的货位ID locationId ：{}", getlocationId);
			getreviewLocationId=result.getString("reviewLocationId");
			logger.info("提取出来的盘点货位（任务）ID reviewLocationId：{}", getreviewLocationId);
			getbarCode=result.getString("barCode");
			logger.info("提取出来的商品条码barCode ：{}", getbarCode);
			getbatchId=result.getString("batchId");
			logger.info("提取出来的生产批次ID	batchId ：{}", getbarCode);
			getgoodsId=result.getString("goodsId");
			logger.info("提取出来的商品ID	goodsId ：{}", getbarCode);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return getlocationId;
	}

	/**
	 * 
	 * 领取详情-扫码
	 */
	public void Scan_code() {
		String Scan_codepath = "/wms/review/get-stock";
//		String code = "6970073992103";
//		String goodsId = "40595711812702209";
		
		StringBuilder Scan_codeparam = new StringBuilder();
		Scan_codeparam.append("{");
		Scan_codeparam.append("\"code\":" + getbarCode);
		Scan_codeparam.append(",");
		Scan_codeparam.append("\"locationId\":" + getlocationId);
		Scan_codeparam.append(",");
		Scan_codeparam.append("\"goodsId\":" + getgoodsId);
		Scan_codeparam.append("}");
		String Scan_code = Scan_codeparam.toString();
		try {
			logger.info("开始进行--------------扫码-----------------");
			result = BuildHttpClient.doPost(Scan_codepath, Scan_code);
			logger.info("这里是\"RF扫描\"响应：{}", result);
		} catch (Exception e) {

		}

	}
}
